package com.example.demo;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@SpringBootApplication
@Controller
@EnableAutoConfiguration
public class DemoApplication {

    private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    public static void main(String[] args) {
        logger.info("{} ", "------main------");
        try {
            int i = 4 / 0;
        } catch (Exception e) {
            logger.error("Error",e);
        }
        logger.info(System.getProperty("java.io.tmpdir"));
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping("/")
    @ResponseBody
    String home(HttpServletRequest request, User user) {
        logger.info("user:{}",user);
        logger.info("RemoteHost123:{}", request.getRemoteHost());
        return "Hello World123!";
    }
    @Bean
    public CommandLineRunner commandLineRunner(final ApplicationContext ctx) {
        return new CommandLineRunner(){

            @Override
            public void run(String... strings) throws Exception {

                    System.out.println("Let's inspect the beans provided by Spring Boot:");

                    String[] beanNames = ctx.getBeanDefinitionNames();
                    Arrays.sort(beanNames);
                    for (String beanName : beanNames) {
                        System.out.println(beanName);
                    }

            }
        };
    }
}
